﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Globalization;

namespace BieuDoGiaVang
{
    public partial class BieuDoVang : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                return;
            }
            //'STYLE GRIDVIEW
            gvGiaVang.Font.Name = "Arial";
            gvGiaVang.RowStyle.BackColor = System.Drawing.Color.AliceBlue;
            gvGiaVang.AlternatingRowStyle.BackColor = System.Drawing.Color.LightBlue;
            gvGiaVang.GridLines = GridLines.Horizontal;
            gvGiaVang.BorderStyle = BorderStyle.Solid;
            gvGiaVang.BorderColor = System.Drawing.Color.Black;
            gvGiaVang.CellPadding = 5;

            this.txtFrom.Text = DateTime.Now.AddDays(-6).ToShortDateString();
            this.txtTo.Text = DateTime.Now.ToShortDateString();

            //Update grid            
            dsGiaVangTableAdapters.VangSJCTableAdapter ad = new dsGiaVangTableAdapters.VangSJCTableAdapter();
            gvGiaVang.DataSource = ad.GetVangSJCByNgay(this.txtFrom.Text, this.txtTo.Text);
            gvGiaVang.DataBind();

            //Chart
            LoadChart();
        }

        protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            e.InputParameters["From"] = DateTime.Now.AddDays(-7).ToShortDateString();
            e.InputParameters["To"] = DateTime.Now.ToShortDateString();            
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            //Case: Date equal empty do nothing
            if (this.txtFrom.Text.Trim() == "" || this.txtTo.Text.Trim() == "")
            {
                return;
            }

            //Update grid            
            dsGiaVangTableAdapters.VangSJCTableAdapter ad = new dsGiaVangTableAdapters.VangSJCTableAdapter();
            gvGiaVang.DataSource = ad.GetVangSJCByNgay(this.txtFrom.Text, this.txtTo.Text);
            gvGiaVang.DataBind();

            //Update Chart
            LoadChart();
        }

        private void LoadChart()
        {
            double minValue = double.MaxValue;
            double maxValue = 0;
            foreach (DataRow item in (gvGiaVang.DataSource as DataTable).Rows)
            {
                //MinValue
                if (Convert.ToDouble(item["GiaMua"]) < minValue)
                {
                    minValue = Convert.ToDouble(item["GiaMua"]);
                }
                if (Convert.ToDouble(item["GiaBan"]) < minValue)
                {
                    minValue = Convert.ToDouble(item["GiaBan"]);
                }
                //MaxValue
                if (Convert.ToDouble(item["GiaMua"]) > maxValue)
                {
                    maxValue = Convert.ToDouble(item["GiaMua"]);
                }
                if (Convert.ToDouble(item["GiaBan"]) > maxValue)
                {
                    maxValue = Convert.ToDouble(item["GiaBan"]);
                }
            }

            //Get MinValue
            chtGiaVang.ChartAreas[0].AxisY.Minimum = minValue-100;
            //Get MaxValue
            chtGiaVang.ChartAreas[0].AxisY.Maximum = maxValue+100;

            using (SqlConnection conn = new SqlConnection())
            {
                SqlCommand cmd = new SqlCommand();
                SqlDataReader reader = null;
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["GiaVangConnectionString"].ConnectionString;
                cmd.Connection = conn;
                cmd.CommandText = "SELECT Ngay,GiaMua,GiaBan From VangSJC WHERE NGAY BETWEEN @FROM AND @TO";
                cmd.Parameters.Add("@FROM", ConvertDateTimeToUs_English(this.txtFrom.Text));
                cmd.Parameters.Add("@TO", ConvertDateTimeToUs_English(this.txtTo.Text));
                conn.Open();
                reader = cmd.ExecuteReader();
                chtGiaVang.Series["GiaMua"].XValueMember = "Ngay";
                chtGiaVang.Series["GiaMua"].YValueMembers = "GiaMua";
                chtGiaVang.Series["GiaBan"].XValueMember = "Ngay";
                chtGiaVang.Series["GiaBan"].YValueMembers = "GiaBan";
                chtGiaVang.DataSource = reader;
                chtGiaVang.DataBind();
                conn.Close();
            }
        }

        private string ConvertDateTimeToUs_English(string date)
        {
            string[] arrStr = date.Split(new string[] { System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.DateSeparator }, StringSplitOptions.RemoveEmptyEntries);
            DateTime dt = new DateTime(Convert.ToInt16(arrStr[2]), Convert.ToInt16(arrStr[1]), Convert.ToInt16(arrStr[0]));

            CultureInfo ci = new CultureInfo("en-US");
            return dt.ToString("d", ci);
        }
    }
}